using System;
using System.Linq.Expressions;
using System.Windows.Controls;
using System.Windows.Data;
using FluentSilverlight.Commands;

namespace FluentSilverlight.Binders
{
    public class TextBoxBinder<TModel> : ControlBinder<TextBoxBinder<TModel>, TextBox, TModel> 
        where TModel : class
    {
        public TextBoxBinder(TModel model, TextBox control)
            : base(model, control)
        {
        }

        public TextBoxBinder<TModel> IsReadOnly(Expression<Func<TModel, object>> expression)
        {
            propertyBinder.SetBinding(expression, TextBox.IsReadOnlyProperty);
            return this;
        }

        public TextBoxBinder<TModel> Text(Expression<Func<TModel, object>> expression)
        {
            propertyBinder.SetBinding(expression, TextBox.TextProperty, BindingMode.TwoWay);
            return thisInstance();
        }

        public TextBoxBinder<TModel> OnTextChange<T>(Expression<Func<TModel, DelegateCommand<T>>> expression)
        {
            var command = PrepareCommand(expression, false);
            control.TextChanged += (sender, e) => command.Execute(control.Text);
            return thisInstance();
        }

        protected override TextBoxBinder<TModel> thisInstance()
        {
            return this;
        }
    }
}